package data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import logic.Answer;
import logic.Course;
import logic.Degree;
import logic.Question;
import logic.Test;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;

public class API_Helper extends AsyncTask<String, Integer, String> {
	
	final String BASE_URL = "http://unitest.net46.net/index.php/api";
	final String UNIVERSITY_RSC = "/universities";
	final String DEGREES_RSC = "/degrees";
	final String COURSES_RSC = "/courses";
	final String TESTS_RSC = "/tests";
	final String QUESTIONS_RSC = "/questions";
	final String ANSWERS_RSC = "/answers";
	
	private String getUniversityUrl () {
		return BASE_URL + UNIVERSITY_RSC;
	}
	
	private String getDegreesUrl (String uniId) {
		return BASE_URL + UNIVERSITY_RSC + "/" + uniId + DEGREES_RSC;
	}
	
	private String getCoursesUrl (String degId) {
		return BASE_URL + DEGREES_RSC + "/" + degId + COURSES_RSC;
	}
	
	private String getTestsUrl (String couId) {
		return BASE_URL + COURSES_RSC + "/" + couId + TESTS_RSC;
	}
	
	private String getQuestionsUrl (String testId) {
		return BASE_URL + TESTS_RSC + "/" + testId + QUESTIONS_RSC;
	}
	
	private String getAnswersUrl (String questId) {
		return BASE_URL + QUESTIONS_RSC + "/" + questId + ANSWERS_RSC; 
	}
	
	private String getApiJson(String url) {
		StringBuilder builder = new StringBuilder();
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			if (statusCode == 200) {
				HttpEntity entity = response.getEntity();
				InputStream content = entity.getContent();
				BufferedReader reader = new BufferedReader(
						new InputStreamReader(content));
				String line;
				while ((line = reader.readLine()) != null) {
					builder.append(line);
				}
				int a = 1;
			} else {
				System.out
						.println("API_Helper: error while getting information");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return builder.toString();
	}

	@Override
	protected String doInBackground(String... urls) {
		return getApiJson(urls[0]);
	}

	@Override
	protected void onPostExecute(String result) {
		// showToast("DONE");
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		System.out.println(values[0] + "%");
	}

	public List<Degree> getDegrees(String uniId) {
		String url = BASE_URL + UNIVERSITY_RSC + "/" + uniId + DEGREES_RSC;
		String json = getApiJson(url);
		return Degree.getDegreesFromJSON(json);
	}

	public List<Test> getTests(String courseId) {
		String url = getTestsUrl(courseId);
		String json = getApiJson(url);
		return Test.getTestsFromJSON(json);
	}

	public List<Course> getCourses(String degreeId) {
		String url = getCoursesUrl(degreeId);
		String json = getApiJson(url);
		return Course.getCoursesFromJSON(json);
	}

	public List<Question> getQuestions(String testId) {
		String url = getQuestionsUrl(testId);
		String json = getApiJson(url);
		return Question.getQuestionsFromJSON(json);
	}

	public List<Answer> getAnswers(String questionId) {
		String url = getAnswersUrl(questionId);
		String json = getApiJson(url);
		return Answer.getAnswersFromJSON(json);
	}
	
	public Test getTest(String testId) {
		String url = BASE_URL + TESTS_RSC + "/" + testId;
		String json = getApiJson(url);
		Test test = Test.getSingleTestFromJSON(json);
		
	    List<Question> questions = getQuestions(testId);
	    for (Question q : questions) {
	    	q.setAnswers(getAnswers(q.getId()));
	    }
	    
	    test.setQuesions(questions);
	    return test;
	}
}
